package com.fc.v2.common.quartz.task;
import cn.hutool.core.date.DateUtil;
import com.fc.v2.mapper.auto.TsysUserMapper;
import com.fc.v2.model.auto.TsysUser;
import com.fc.v2.model.auto.TsysUserExample;
import com.fc.v2.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 *测试类
 * @CLASSNAME   V2Task
 * @Description 定时调度具体工作类
 * @Auther Jan  橙寂
 * @DATE 2019/9/2 0002 15:33
 */
@Component("v2Task")
public class V2Task {
	@Autowired
	private TsysUserMapper tsysUserMapper;

    private static final Logger log = LoggerFactory.getLogger(V2Task.class);
    /**
     * 无参的任务
     */
    public void runTask1()
    {
        System.out.println("正在执行定时任务，无参方法");
    }

    /**
     * 有参任务
     * 目前仅执行常见的数据类型  Integer Long  带L  string  带 ''  bool Double 带 d
     * @param a
     * @param b
     */
    public void runTask2(Integer a,Long b,String c,Boolean d,Double e)
    {
    	List<TsysUser> list=  tsysUserMapper.selectByExample(new TsysUserExample());
    	System.err.println("用户查询num："+list.size());
        System.out.println("正在执行定时任务，带多个参数的方法"+a+"   "+b+" "+c+"  "+d+" "+e+"执行时间:"+DateUtil.now());
    }

    /**
     * 备份数据库
     * @param mysqlPath mysql路径
     * @param username
     * @param password
     * @param databasename
     * @throws Exception
     */
    //mysqldump -h端口号 -u用户 -p密码 数据库 > d:/test.sql --备份D盘
    //备份
    public static void dataBaseDump(String mysqlPath,String username,String password,String databasename) throws Exception {
        File file = new File("D:\\_Document\\fanmini\\backup");
        if (!file.exists()) {
            file.mkdir();
        }
        String fileName = databasename + "_" + DateUtils.getDate() + "_backup";
        File datafile = new File(file + File.separator + fileName + ".sql");
        if (datafile.exists()) {
            log.error(fileName + "文件名已存在，请更换");
            return;
        }

        //拼接cmd命令
        //此处mysqldump需要为绝对路径，环境变量这里没法识别
//        D:\mysql-5.7.31-winx64
        log.debug("mysqlPath:"+mysqlPath);
        String s = "cmd /c "+mysqlPath+"\\bin\\mysqldump -hlocalhost -u" + username + " -p" + password + " --opt " + databasename + " > " + datafile;
        log.info("数据库备份命令：{}", s);
        Process exec = Runtime.getRuntime().exec(s);

        if (exec.waitFor() == 0) {
            log.info("数据库备份成功,备份路径为：" + datafile);
        } else {
            log.error("数据库备份失败,备份路径为：" + datafile + "备份命令为：" + s);
        }
    }
}
